package handler

import (
	"github.com/gin-gonic/gin"
	"pic-text-mgr/models"
	"pic-text-mgr/pkg"
)

func Login(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	var user models.User
	if err := models.DB.Where("tel=?", tel).Find(&user).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号不存在",
		})
		return
	}
	if pkg.Md5(password) != pkg.Md5(user.Password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	jwt := pkg.TokenHandler(user.ID)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"jwt":  jwt,
	})
	return
}
